package com.jadefashion.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.service.ReportService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("export")
public class ExpostController {
    @Reference
    private ReportService reportService;

    //下载运营数据
    @RequestMapping("exportExcel.do")
    @PreAuthorize("hasAuthority('REPORT_VIEW')")
    public void exportExcel(HttpSession session, HttpServletResponse response) throws IOException {
        String realPath = session.getServletContext().getRealPath("/template/report_template.xlsx");
        TemplateExportParams templateExportParams = new TemplateExportParams(realPath);
        Map map = reportService.findBusinessData();
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams,map);
//        File file = new File("d:/cd/report_template.xlsx");
        String mimeType = session.getServletContext().getMimeType("11.xlsx");
        response.setContentType(mimeType);
        response.setHeader("content-Disposition","attachment;filename=report_template.xlsx");
        ServletOutputStream os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        os.close();
        workbook.close();
    }
}
